#include ("/_includes/_layout.html")
#define css()
<link rel="stylesheet" type="text/css" href="#(webctx)/resources/css/full_cut_css.css"/>
#end
#@layout("满减送", "wxmall,点步科技", "Wxmall社区","wxmall") 
#define content()
<div class="wrapper wrapper-content">
	<div class="ibox-content">
		<div class=" active_statistics col-sm-12">
		<div class="title_query padtop">
		    <!-- <div class="ibox-title">
			  <small><a href="#(webctx)/fullcut">《返回列表</a></small>
		    </div> -->
			<form id="cashbackForm" class="form-horizontal">
				<input type="hidden" name="id" id="id" value="#(fullCutDto.id??)"/>
				<div class="form-group">
		            <label class="col-md-2 control-label">活动名称<span style="color: red;"><em>*</em></span></label>
		            <div class="col-md-8" id="error_name">
		                <input id="name" name= "name" value="#(fullCutDto.name??)" maxlength="20" type="text" class="form-control" style="width: 400px" >
		                <label class="control-label" for="name"></label>
		            </div>
		        </div>
		        <div class="form-group">
		            <label class="col-md-2 control-label">生效时间<span style="color: red;"><em>*</em></span></label>
		            <div class="col-sm-9">
	                   	<div class="row">
	                   		<div class="col-md-3" id="error_start_time">
		                        <input type="text" id="start_date" name="start_time" readonly="readonly" style="width: 160px;"
								onfocus="WdatePicker({errDealMode:2, skin:'twoer', isShowClear:false,readOnly:true,dateFmt:'yyyy-MM-dd %H:%m:%s',minDate:'%y-%M-%d %H:%m:%s',maxDate:'#F{$dp.$D(\'end_date\')}'})"
								class="Wdate" value="#date(fullCutDto.startDate??, 'yyyy-MM-dd HH:mm:ss')"/>
								<label class="control-label" for="start_date"></label>
		                    </div>
		                    <div class="col-md-3" id="error_end_time">
		                        <input type="text" id="end_date" name="end_time" readonly="readonly" style="width: 160px;"
								onfocus="WdatePicker({errDealMode:2, skin:'twoer', isShowClear:false,readOnly:true,dateFmt:'yyyy-MM-dd %H:%m:%s',minDate:'#F{$dp.$D(\'start_date\',{d:7});}'})"
								class="Wdate" value="#date(fullCutDto.endDate??, 'yyyy-MM-dd HH:mm:ss')" />	
								<label class="control-label" for="end_date"></label>
		                    </div>	
		                    <div class="col-md-4">
		                    	<a class="label label-default" href="javascript:dateTimeTag(7);">一周</a>
		                    	<a class="label label-default" href="javascript:dateTimeTag(15);">半个月</a>
		                    	<a class="label label-default" href="javascript:dateTimeTag(30);">一个月</a>
		                    </div>
		                </div>
		            </div>
		        </div>
		        
		        <div class="form-group">
		            <label class="col-md-2 control-label">优惠条件<span style="color: red;"><em>*</em></span></label>
		            <div class="col-md-9 page-fullcut" id="error_name">
		               <div class="fullcut-table-wrap">
							<table class="fullcut-table">
								<thead>
									<tr>
										<th width="35%">优惠门槛</th>
										<th class="pl100" width="55%">优惠方式(可多选)</th>
										<th width="10%">操作</th>
									</tr>
								</thead>
							  <tbody id="fullcut-condition">
							  #if(fullCutDto)
							  #for(setItem : fullCutDto.sets)
							  <tr>
							   <input class="span form-control" name="set_id" value="#(setItem.id)" type="hidden"/>
									<td align="center">
										<!-- <div class="control-group">
										<div class="controls" style="margin:0;">
										 <span>满</span>
										<input class="span form-control" name="meet" value="#(setItem.meet)" type="text" style="width: 80px;" onkeyup="this.value=this.value.replace(/[^\d.]/g,'')">
										<span>元</span> -->
										<div class="input-group">
										<span class="input-group-addon">满</span>
										<input type="text" value="#(setItem.meet)" name="meet" class="form-control" style="width: 70px;" onkeyup="this.value=this.value.replace(/[^\d.]/g,'')">
										<span class="input-group-addon">元</span>
										</div> 
									   <!-- </div>
										</div> -->
									</td>
									<td>
									<!--  <div class="control-group fullcut-setting first-fullcut"> -->
										<div class="controlss">
										<label class="checkbox inline fullcut-label js-trigger-label">
										<input class="checked-status" name="cash_required" type="checkbox" value="1" onclick="checkStatus(this);" #if(setItem.cash_required==1) checked="checked" #end>
										
										#if(setItem.cash_required==1)
										<span class="origin-status " style="display: none;">减现金</span>
										<div class="replace-status input-group">
										<span class="input-group-addon">减</span>
										<input type="text" value="#(setItem.cash)" name="cash" class="form-control" style="width: 70px;" onkeyup="this.value=this.value.replace(/[^\d.]/g,'')">
										<span class="input-group-addon">元</span>
										</div> 
										<!--  <span class="replace-status js-response-label">
										减
										<input class="span1 js-valid-input form-control" name="cash" value="#(setItem.cash)" style="width: 80px;" type="text" onkeyup="this.value=this.value.replace(/[^\d.]/g,'')">
										元
										</span> -->
										#else
										 <span class="origin-status " style="display: inline-block;">减现金</span>
										<!--  <span class="replace-status js-response-label" style="display: none;">
										减
										<input class="span1 js-valid-input form-control" name="cash" value="#(setItem.cash)" style="width: 80px;" type="text" onkeyup="this.value=this.value.replace(/[^\d.]/g,'')">
										元
										</span> -->
										<div class="replace-status input-group" style="display: none;">
										<span class="input-group-addon">减</span>
										<input type="text" value="#(setItem.cash)" name="cash" class="form-control" style="width: 70px;" onkeyup="this.value=this.value.replace(/[^\d.]/g,'')">
										<span class="input-group-addon">元</span>
										</div> 
										#end
										</label>
										</div>
										<!-- 
									</div> -->
									<div class="control-group fullcut-setting">
										<div class="controls">
										<label class="checkbox inline fullcut-label">
										<input class="checked-status" name="postage" type="checkbox" value="1" #if(setItem.postage==1) checked="checked" #end>
										<span class="origin-status">免邮</span>
										</label>
										</div>
									</div>
									<label class="status_error_msg" style="color:red;margin-left: 70px;"></label>
									</td>
									<td align="center">
									<a class="gray js-remove-item" data-id="#(setItem.id)">删除</a>
									</td>
								  </tr>
								#end
								#else 
							    <tr>
							        <input class="span form-control" name="set_id" value="" type="hidden">
									<td align="center">
										<div class="input-group">
										<span class="input-group-addon">满</span>
										<input type="text" value="" name="meet" class="form-control" style="width: 70px;" onkeyup="this.value=this.value.replace(/[^\d.]/g,'')">
										<span class="input-group-addon">元</span>
										</div> 
									</td>
									<td>
										<div class="controlss">
											<label class="checkbox inline fullcut-label js-trigger-label">
											<input class="checked-status" name="cash_required" type="checkbox" value="1" onclick="checkStatus(this);">
											<span class="origin-status " style="display: inline-block;">减现金</span>
											<div class="replace-status input-group" style="display: none;">
											<span class="input-group-addon">减</span>
											<input type="text" value="" name="cash" class="form-control" style="width: 70px;" onkeyup="this.value=this.value.replace(/[^\d.]/g,'')">
											<span class="input-group-addon">元</span>
											</div>
											</label>
										</div>
									<div class="control-group fullcut-setting">
										<div class="controls">
										<label class="checkbox inline fullcut-label">
										<input class="checked-status" name="postage" type="checkbox" value="1">
										<span class="origin-status">免邮</span>
										</label>
										</div>
									</div>
									<label class="status_error_msg" style="color:red;margin-left: 70px;"></label>
								    </td>
								  </tr>
								  #end
							  </tbody>
							 <tfoot class="add-fullcut-toolbar" style="display: table-footer-group;">
                             <tr> 
							  <td colspan="5">
	                            <div class="fullcut-controls">
	                            <a href="javascript:addfullcut()" class="js-add-fullcut">+新增优惠</a>
	                            <!-- <span class="gray pl20">最多可设置五个层级</span> -->
	                           </div>
                              </td>
                              </tr>
                              </tfoot>
						</table> 
		            </div>
		        </div>
		      </div>   
		      
			 <div class="panel-body">
			 	<ol class="breadcrumb">
					<li><a class="btn btn-w-m btn-primary" style="color: white;" href="javascript:selectProduct();">选择活动商品</a></li>
				</ol>
			 	<br>
                <table class="table table-striped table-bordered bootstrap-datatable datatable"  border="0" style="width: 80%">
					<thead>
						<tr>
							<th><span style="vertical-align: middle;">&nbsp;&nbsp;商品</span></th>
							<th>价格</th>
							<th>库存</th>
							<th>操作</th>
						</tr>
					</thead>
					<tbody class="seletctProductLotGrid"></tbody>
				</table>
			 </div>	
		  </form>
		  
		  <div class="panel-footer" align="center">
		   	 	<button type="button" onclick="saveCashback();" class="btn btn-sm btn-success"><i class="fa fa-dot-circle-o"></i>提交</button>
		   	 	<a href="#(webctx)/fullcut" class="btn btn-default btn-sm">取   消</a>
			</div>
		 </div>
		</div>
		<div class="col-sm-2">
			<div style="margin-top: 50px;"></div>
            <!-- <div class="project-manager" style="padding-left: 10px;padding-top: 10px;">
                <h4>帮助说明</h4>
                <ol>
                    <li>活动设置后会出现在商品详情页</li>
                    <li>可灵活设置满包邮，满多少减现金，在活动有效时间内，买家下单的时候，会立即根据规则获取优惠</li>
                </ol>
            </div> -->
        </div>
	</div>
</div>

<div class="modal fade bs-example-modal-lg" id="selectProduct">
	<div class="modal-dialog modal-lg">
		<div class="modal-content ">
			<div class="modal-header">
				<button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
				<h4 class="modal-title">选择活动商品</h4>
			</div>
			<div class="modal-body">    
		       		<div id="myTabContent" class="tab-content">
						<div class="tab-pane active" id="select_items_div">
							<table class="table table-striped table-bordered bootstrap-datatable datatable" id="mainTable" border="0" style="width: 100%">
								<thead>
									<tr>
										<th><input type="checkbox" id="all" title="全选/反选" style="margin-right: 4px; vertical-align: middle;"/></th>
										<th><span style="vertical-align: middle;">&nbsp;&nbsp;商品</span></th>
										<th>价格</th>
										<th>库存</th>
										<!-- <th>操作</th> -->
									</tr>
								</thead>
								<tbody class="productLotGrid">
								</tbody>
							</table>
							<div id="productToolsbar" class="panel-body">
								<!-- <div  style="display: block;margin-left: 20px" > -->
									<button id="selectAllProduct" type="button" class="btn btn-sm btn-primary">确      定</button>	
								<!-- </div> -->
								<div id="pager" class="jqpager" style="margin-bottom: 1px;float: right;"></div>
							</div>
						</div>
					</div>
			</div>
			<!-- <div class="modal-footer" >  align="center"-->
				
        	<!-- </div> -->
		</div>
	</div>
</div>

<!-- 选择的商品列表模板 -->
<script type="text/template" id="product_table_tr_tpl">
		<tr class="package">
			<td><input type="checkbox" class="commchk" id="checkbox_{id}" product_id="{id}" product_name="{name}" product_price="{price}" product_img="{img}" style="margin-right: 4px; vertical-align: middle;"/></td>
			<td>
			<ul class="js-picture-lists app-image-list"><li><img src="{img}" width="40px" height="40px"></li></ul>
            <p class="goods-title"><a class="new-window" href="javascript:void(0)" title="{name}">{name}</a></p>
			</td>
			<td><span class="label label-success">￥{price}</span></td>
            <td>{stock}</td>
		</tr>
</script>

<script type="text/template" id="select_product_table_tr_tpl">
		<tr id="{id}" class="package">
			<td>
			<ul class="js-picture-lists app-image-list"><li><img src="{img}" width="40px" height="40px"></li></ul>
            <p class="goods-title"><a class="new-window" href="javascript:void(0)" title="{name}">{name}</a></p>
			</td>
			<td><span class="label label-success">￥{price}</span></td>
            <td>{stock}</td>
            <td><a id="{id}" class="btn btn-success" href="javascript:void(0)">取消</a></td>
		</tr>
</script>	

<script type="text/template" id="fullcut_tr_tpl">
<tr>
 <input class="span form-control" name="set_id" value="" type="hidden">
<td align="center">
		<div class="replace-status input-group"">
			<span class="input-group-addon">满</span>
			<input type="text" value="" name="meet" class="form-control" style="width: 70px;" onkeyup="this.value=this.value.replace(/[^\d.]/g,'')">
			<span class="input-group-addon">元</span>
		</div>
</td>
<td>
	<div class="controlss">
	<label class="checkbox inline fullcut-label js-trigger-label">
	<input class="checked-status" name="cash_required" type="checkbox" value="1" onclick="checkStatus(this);">
	<span class="origin-status " style="display: inline-block;">减现金</span>
	<div class="replace-status input-group" style="display: none;">
										<span class="input-group-addon">减</span>
										<input type="text" value="" name="cash" class="form-control" style="width: 70px;" onkeyup="this.value=this.value.replace(/[^\d.]/g,'')">
										<span class="input-group-addon">元</span>
										</div> 
	</label>
	</div>
<div class="control-group fullcut-setting">
	<div class="controls">
	<label class="checkbox inline fullcut-label">
	<input class="checked-status" name="postage" type="checkbox" value="1">
	<span class="origin-status">免邮</span>
	</label>
	</div>
</div>
<label class="status_error_msg" style="color:red;margin-left: 70px;"></label>
</td>
<td align="center">
<a class="gray js-remove-item">删除</a>
</td>
</tr>
</script>	 
#end
<script type="text/javascript">
var itemsAll = new Array();
function saveCashback(){
	var params = {}, error = {};
	var  name = $("#name").val(),start_date = $("#start_date").val(), end_date = $("#end_date").val(),id=$("#id").val();
	params.id=id;
	if($.trim(name) =="") {error.error_name = "活动名称不能为空"; } else {error.error_name=""; params.name = $.trim(name);}
	if($.trim(start_date) =="") {error.error_start_time = "开始时间不能为空"; } else {error.error_start_time =""; params.start_date = $.trim(start_date);}
	if($.trim(end_date) =="") {error.error_end_time = "结束时间不能为空"; } else { error.error_end_time=""; params.end_date = $.trim(end_date);}
	var hasErr = false;
	$("#fullcut-condition tr").each(function(i,obj){
 		var meet = $(obj).find("input[name='meet']");
 		if(meet.val()==""){	
 			if(!hasErr) hasErr=true;
 			meet.css("border-color", "#ff5454");
		}
 		
 		if($(obj).find("input[name='cash_required']").is(':checked')||$(obj).find("input[name='postage']").is(':checked')){
 			
 		}else{
 			if(!hasErr) hasErr=true;
 			$(obj).find(".status_error_msg").html("优惠方式不能为空");
 		}
 		
 		if($(obj).find("input[name='cash_required']").is(':checked')){
 			var cash = $(obj).find("input[name='cash']");
 	 		if(cash.val()==""){	
 	 			if(!hasErr) hasErr=true;
 	 			cash.css("border-color", "#ff5454");
 			}
 		}
 		
	});
	for(var key in error){
		if(error[key]!=""){
			if(!hasErr) hasErr = true;
			$("#"+key).addClass("has-error");
			$("#"+key).find("label").text(error[key]);
		}else{
			$("#"+key).removeClass("has-error");
			$("#"+key).find("label").empty();
		}
	}
	//主表信息不完整，返回
	if(hasErr) return false;
	
	$("#fullcut-condition tr").each(function(i,obj){
		var meet = $(obj).find("input[name='meet']").val();
		var set_id = $(obj).find("input[name='set_id']").val();
		var cash="";
		var cash_required="";
		var postage="";
		if($(obj).find("input[name='cash_required']").is(':checked')){
		cash_required=$(obj).find("input[name='cash_required']:checked").val();
 		cash = $(obj).find("input[name='cash']").val();
		}
		if($(obj).find("input[name='postage']").is(':checked')){
			postage=$(obj).find("input[name='postage']:checked").val();
		}
		 var entity = new Object();
		    entity.id=set_id;
		    entity.meet=meet;
			entity.cash_required = cash_required;
			entity.postage = postage;
			entity.cash= cash;
			entity.opt = $(this).attr("opt");
			itemsAll.push(entity);	 
	});
	params.setItem=JSON.stringify(itemsAll);
	if($("#fullcut-condition").find("tr").length<=0){
		obz.info("请新增优惠!");
		return false;
	}
	if($(".seletctProductLotGrid").find("tr").length<=0){
		obz.warn("请选择商品!");
		return false;
	}else{
		var productIds="";
		$(".seletctProductLotGrid").find("tr").each(function(){
			productIds=productIds+$(this).attr("id")+"-";
		});
		params.product_ids=productIds;
	}

	$(".float-e-margins").mask("正在提交数据...");
	$.post(obz.ctx+"/fullcut/save", params, function(resp) {
		if(resp.code != 200){
			obz.error(resp.msg, function(){
				$(".float-e-margins").unmask();
			});
			return;
		}
		obz.msg("保存成功", function(){
			$(".float-e-margins").unmask();
			location.href = obz.ctx + "/fullcut";
		});
	});
}
$(document).ready(function(){
	bindSetTableAEvent();
	var productIds="#(fullCutDto.productIds??)"
	if(productIds!=null&&productIds!=""){
		getProducts(productIds);
	}
	$("#selectAllProduct").click(function(){
		if(TBatch.getCheckedCount() <=0){
			obz.info("请选择商品!");
			return false;
		}else{
			 $("#selectProduct").modal("hide");	
			var product_ids=TBatch.getChecked();
			product_ids=product_ids.substring(0,product_ids.length-1);
			var ids="";
			var item=product_ids.split("-");
			for (var i = 0; i < item.length; i++) {
				if($(".seletctProductLotGrid").find("tr").length>0){
					var isExist = false;
					$(".seletctProductLotGrid").find("tr").each(function(){
						var productIds=$(this).attr("id");
						if(productIds==item[i]){
							 isExist = true;
					         return false;
						}
					});
					if(!isExist){
						ids=ids+item[i]+"-";
					}
				}else{
					ids=ids+item[i]+"-";
				}	
			}
			getProducts(ids);
		}
	});
});
//新增优惠
function addfullcut(){
	$("#fullcut-condition").append($("#fullcut_tr_tpl").html());
	bindSetTableAEvent();
}
//是否减现金
function checkStatus(obj){
	if($(obj).is(':checked')) {
		$(obj).parent().find(".origin-status").hide();
		$(obj).parent().find(".replace-status").show();
	}else{
		$(obj).parent().find(".origin-status").show();
		$(obj).parent().find(".replace-status").hide();
	}
}
//列出商品
function selectProduct(){
	 $("#selectProduct").modal("show");
}
function bindSetTableAEvent(){
	//注册取消返现商品事件
	$("#fullcut-condition").find("tr a").each(function(){
		$(this).unbind("click");
		$(this).click(function(){
			var me = $(this);
			var id=me.attr("data-id");
			//取消设置不能直接删除该行，需要隐藏
			if(id){
				me.parent().parent().attr("opt", "del");
				me.parent().parent().hide();	
			}else{
				me.parent().parent().remove();
			}
			return false;
		});
	});
}
function bindSelectProdTableAEvent(){
	$(".seletctProductLotGrid").find("tr a").each(function(){
		$(this).unbind("click");
		$(this).click(function(){
			var me = $(this);
			me.parent().parent().remove();
			return false;
		});
	});
}
var getProducts = function(productIds){
	if(productIds!=''&&productIds!=null){
		var params = {};
		params.productIds=productIds;
		$("#mainTable").mask("加载中...");
		var url = obz.ctx + "/cashback/listSelectProduct";
		obz.ajaxJson(url, params, function(resp){
			$("#mainTable").unmask();
			//$(".seletctProductLotGrid").empty();
			var dataList = resp.data;
			if(dataList.length>0){
				for(var i=0;i<dataList.length;i++){
					var _row = dataList[i];
					var trHtml = obz.dataTemplate4obj($("#select_product_table_tr_tpl").html(), _row);
					$(".seletctProductLotGrid").append(trHtml);
					bindSelectProdTableAEvent();
				}
			}
		});
	}
}
var params = {};
var table = new obz.TableView("pager", obz.ctx + "/fullcut/listProducts", params, function(resp){
	$(".productLotGrid").empty();
	if(resp && resp.list && resp.list.length>0){
		var dataList = resp.list;
		if(dataList.length>0){
			for(var i=0;i<dataList.length;i++){
				var _row = dataList[i];
				var trHtml = obz.dataTemplate4obj($("#product_table_tr_tpl").html(), _row);
				$(".productLotGrid").append(trHtml);
			}
			TBatch.checkCheckbox(function (chkAll){
				$("#mainTable input:checkbox.commchk").each(function(){
					if($(this).is(":checked") == true){
					}else{
					}
				});
			});
			TBatch.initCheckboxClick(function(resp){
			}, function(resp){
			});
		}else{
			$(".productLotGrid").append($("#table_noresult_tr_tpl").html());
		}
	}else{
		$(".productLotGrid").append($("#table_noresult_tr_tpl").html());
	}
});
table.init();
</script>